package com.ttj.util;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

public class BeanUtil {
	
	private static Logger logger = LoggerFactory.getLogger(BeanUtil.class);
	
	/**
	 * 对象列表转换
	 * @param <E>
	 * @param list
	 * @param target
	 * @return
	 */
	public static <E> List<E> voConvertList(List<?> list, Class<E> target) {
		List<E> retList = new ArrayList<E>();
		
		for(Object o: list) {
			E targetObj = voConvert(o, target);
			retList.add(targetObj);
		}
		return retList;
	}
	
	/**
	 * 对象转换
	 * @param o
	 * @param target
	 * @return
	 */
	public static <E> E voConvert(Object o, Class<E> target) {
		E targetObj = null;
		try {
			targetObj = target.newInstance();
			BeanUtils.copyProperties(o, targetObj);
		} catch (Exception e) {
			logger.error("e={}", e);
		}
		return targetObj;
	}
}
